﻿using System;
using Microsoft.Xna.Framework;
using RainEngine.Controllers;
using RainEngine.SceneManagement;
using Microsoft.Xna.Framework.Input;

namespace RainEngine.SceneNodes.UI
{
    /// <summary>
    /// radioButton组
    /// </summary>
    public  class UIRadioButtonGroup:UISceneNode
    {
        /// <summary>
        /// 单选框数组
        /// </summary>
        UIRadioButton[] radioButtons;

        /// <summary>
        /// 各radioButton之间的垂直间距。
        /// </summary>
        float verticalSpace = 10.0f;        

        /// <summary>
        /// 被选中的单选框索引，初始时为-1，即没有单选框被选中。
        /// </summary>
        int selectedIndex = -1;       

        /// <summary>
        /// 创建一组单选框
        /// </summary>        
        /// <param name="engine">引擎</param>
        /// <param name="setScene">控件所属的scene</param>
        /// <param name="setPosition">2D屏幕位置</param>
        /// <param name="radiobutton">单选框数组</param>
        public UIRadioButtonGroup(RainGE engine, Scene setScene, Vector2 setPosition, UIRadioButton[] radiobuttons)
            : base(engine,setScene ,setPosition ,null,false )
        {
            //UIRadioButtonGroup没有焦点
            this.TabStop = false;
            
            this.radioButtons = radiobuttons;
            
            //向下依次放置各个radioButton
            float radioButtonY = 0;            
            for (int i = 0; i < radiobuttons.Length; i++)
            {
                radiobuttons[i].CheckedChanged+=new EventHandler(OnCheckedChanged);
                radiobuttons[i].CanUncheck = false;
                if (radiobuttons[i].Checked  == true)
                    selectedIndex = i;                
                
                radiobuttons[i].Position =new Vector2 (setPosition.X, setPosition.Y + radioButtonY);
                radioButtonY += radiobuttons[i].Size.Y + verticalSpace;                
            }
            
        }        

        #region 属性     

        /// <summary>
        /// 获取或设置各radioButton之间的垂直间距，当此间距发生改变时需要重新调整各radioButton的位置。
        /// </summary>
        public float VerticalSpace
        {
            get { return verticalSpace; }
            set
            {
                verticalSpace = value;
                ResetRadioButtons();
            }
        }

        /// <summary>
        /// 获取或设置被选中的单选框，返回选中单选框上的文字，或根据设置的文字选中单选框
        /// </summary>
        public string Selected
        {
            get
            {
                //遍历单选框数组
                for (int i = 0; i < radioButtons.Length; i++)
                    //如果找到第一个为选中状态的单选框，则返回这个单选框的文字
                    if (radioButtons[i].Checked == true)
                        return radioButtons[i].Text;
                //否则返回""
                return "";
            }
            set
            {
                //遍历单选框数组
                for (int i = 0; i < radioButtons.Length; i++)
                    //如果单选框的文字为设定的文字，则将这个单选框的Checked属性设为true
                    if (radioButtons[i].Text.ToLower() == value.ToLower())
                        radioButtons[i].Checked = true;
                    //将其他单选框的Checked设为false
                    else
                        radioButtons[i].Checked = false;
            }
        }

        #endregion

        public override int Draw(GameTime gameTime)
        {
            return 0;
        }

        #region 事件相关处理程序

        /// <summary>
        /// 选中项改变时引发的事件
        /// </summary>
        public event EventHandler ChangeSelection;

        /// <summary>
        /// 选中项改变时引发的事件的处理程序
        /// </summary>
        private void OnCheckedChanged(object obj, EventArgs e)
        {
            UIRadioButton radiobutton = (UIRadioButton)obj;            
            //将选中的单选框索引重置为-1
            int checkIndex = -1;
            
            //遍历单选框数组找到选中的单选框的索引
            for (int i = 0; i < this.radioButtons.Length; i++)
            {
                if (this.radioButtons[i] == radiobutton)
                {
                    checkIndex = i;                    
                    break;
                }
            }

            //如果当前被选中的单选框索引不等于前面选中的单选框索引，则引发ChangeSelection事件
            if(checkIndex != selectedIndex)
                if (ChangeSelection != null)
                    ChangeSelection(radiobutton.Text, null);

            //重新设置选中的单选框索引
            selectedIndex = checkIndex;
            //将其余单选框的Checked属性设为false
            for (int i = 0; i < this.radioButtons.Length; i++)
                if (i != checkIndex)
                    this.radioButtons[i].Checked  = false;
        }

        /// <summary>
        /// 如果2D位置发生改变则重新放置各radioButton的位置
        /// </summary>
        protected override void OnLocationChanged()
        {
            ResetRadioButtons();
            base.OnLocationChanged();
        }

        /// <summary>
        /// 切换每个radioButton的IsVisible属性
        /// </summary>
        /// <param name="e"></param>
        protected override void OnVisibleChanged(EventArgs e)
        {
            for (int i = 0; i < radioButtons.Length; i++)
                radioButtons[i].Visible = !radioButtons[i].Visible;
            base.OnVisibleChanged(e);
        }

        /// <summary>
        /// 重新设置每个单选框的位置
        /// </summary>
        private void ResetRadioButtons()
        {
            //将radioButton的竖直位置初始化为0            
            float radioButtonY = 0;

            for (int i = 0; i < this.radioButtons.Length; i++)
            {
                //向下依次放置各个radioButton
                radioButtons[i].Position = new Vector2(Position.X, Position.Y + radioButtonY);
                radioButtonY += radioButtons[i].Size.Y + verticalSpace;
            }
        }
        
        #endregion
        
    }
        
}

